#ifndef __DRV_SPI1_H_
#define __DRV_SPI1_H_

#include "stm32f10x.h"

typedef struct{
    SPI_TypeDef* SPIx;
    void (*config)(void);
    void (*deconfig)(void);
    uint8_t (*wr_bytes)(SPI_TypeDef *spix, uint8_t *pdata, uint16_t len);
}spi_context_t;
extern spi_context_t spi2_context;


//SPI1-NFC
#define     NFC_SPIx                            SPI1
#define     NFC_SPI_APBxPeriphClockCmd          RCC_APB2PeriphClockCmd
#define     NFC_SPI_CLK                         RCC_APB2Periph_SPI1

//PA4-CS(NSS)引脚 片选选普通GPIO即可
#define     NFC_SPI_CS_APBxPeriphClockCmd       RCC_APB2PeriphClockCmd
#define     NFC_SPI_CS_CLK                      RCC_APB2Periph_GPIOA    
#define     NFC_SPI_CS_PORT                     GPIOA
#define     NFC_SPI_CS_PIN                      GPIO_Pin_4

//PA5-SCK引脚
#define     NFC_SPI_SCK_APBxPeriphClockCmd      RCC_APB2PeriphClockCmd
#define     NFC_SPI_SCK_CLK                     RCC_APB2Periph_GPIOA   
#define     NFC_SPI_SCK_PORT                    GPIOA   
#define     NFC_SPI_SCK_PIN                     GPIO_Pin_5

//PA6-MISO引脚
#define     NFC_SPI_MISO_APBxPeriphClockCmd     RCC_APB2PeriphClockCmd
#define     NFC_SPI_MISO_CLK                    RCC_APB2Periph_GPIOA    
#define     NFC_SPI_MISO_PORT                   GPIOA 																																							
#define     NFC_SPI_MISO_PIN                    GPIO_Pin_6

//PA7-MOSI引脚
#define     NFC_SPI_MOSI_APBxPeriphClockCmd     RCC_APB2PeriphClockCmd
#define     NFC_SPI_MOSI_CLK                    RCC_APB2Periph_GPIOA    
#define     NFC_SPI_MOSI_PORT                   GPIOA 
#define     NFC_SPI_MOSI_PIN                    GPIO_Pin_7

#define     NFC_SPI_CS_LOW()     		        GPIO_ResetBits(NFC_SPI_CS_PORT, NFC_SPI_CS_PIN )
#define     NFC_SPI_CS_HIGH()    		        GPIO_SetBits(NFC_SPI_CS_PORT, NFC_SPI_CS_PIN )


//SPI2-FLASH
#define     FLASH_SPIx                              SPI2
#define     FLASH_SPI_APBxPeriphClockCmd            RCC_APB1PeriphClockCmd
#define     FLASH_SPI_CLK                           RCC_APB1Periph_SPI2

//PA4-CS(NSS)引脚 片选选普通GPIO即可
#define     FLASH_SPI_CS_APBxPeriphClockCmd         RCC_APB2PeriphClockCmd
#define     FLASH_SPI_CS_CLK                        RCC_APB2Periph_GPIOB    
#define     FLASH_SPI_CS_PORT                       GPIOB
#define     FLASH_SPI_CS_PIN                        GPIO_Pin_12

//PA5-SCK引脚
#define     FLASH_SPI_SCK_APBxPeriphClockCmd        RCC_APB2PeriphClockCmd
#define     FLASH_SPI_SCK_CLK                       RCC_APB2Periph_GPIOB   
#define     FLASH_SPI_SCK_PORT                      GPIOB   
#define     FLASH_SPI_SCK_PIN                       GPIO_Pin_13

//PA6-MISO引脚
#define     FLASH_SPI_MISO_APBxPeriphClockCmd       RCC_APB2PeriphClockCmd
#define     FLASH_SPI_MISO_CLK                      RCC_APB2Periph_GPIOB    
#define     FLASH_SPI_MISO_PORT                     GPIOB																																			
#define     FLASH_SPI_MISO_PIN                      GPIO_Pin_14

//PA7-MOSI引脚
#define     FLASH_SPI_MOSI_APBxPeriphClockCmd       RCC_APB2PeriphClockCmd
#define     FLASH_SPI_MOSI_CLK                      RCC_APB2Periph_GPIOB    
#define     FLASH_SPI_MOSI_PORT                     GPIOB
#define     FLASH_SPI_MOSI_PIN                      GPIO_Pin_15

#define     FLASH_SPI_CS_LOW()                      GPIO_ResetBits(FLASH_SPI_CS_PORT, FLASH_SPI_CS_PIN )
#define     FLASH_SPI_CS_HIGH()    		            GPIO_SetBits(FLASH_SPI_CS_PORT, FLASH_SPI_CS_PIN )

void SPI1_Init(void);
void DEV_WriteSR(uint8_t sr, uint8_t value);
uint8_t DEV_ReadSR(uint8_t sr);


void _ASSERT_SPI_CLK_LOW(void);
void _RELEASE_SPI_CLK_LOW(void);

#endif
